//==================================================================
//  Copyright (C) 2006-2007  Davide Pasca
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//==================================================================
///
///
///
///
//==================================================================

#ifndef GXY_CONTEXT_H
#define GXY_CONTEXT_H

#include "psys.h"
#include "gxy_base.h"
#include "texture_base.h"
#include "gxy_sprite.h"
#include "gxy_font.h"

//==================================================================
namespace GXY
{

//==================================================================
class SystemState
{
public:
	int			_disp_w;
	int			_disp_h;
	float		_disp_aspect;

	float		_viewport[4];

	float		_global_off[2];
	float		_global_sca[2];

	PSYSGFX::BasicBlend	_blend;
	int			_global_alpha_coe;

	float		_sca_x;	// scale affecting vertices
	float		_sca_y;	// scale affecting vertices

	float		_rot_z;
	float		_rot_z_cos, _rot_z_sin;

	int			_font_idx;
	int			_font_is_prop;
	float		_font_sca_x;	// scale affecting font size
	float		_font_sca_y;	// scale affecting font size

	u_char		_outcolverts[4][4];
	u_char		_colverts[4][4];

	PMath::Matrix44	_cur_proj_m;
	PMath::Matrix44	_cur_mview_m;

	//u_int	flags;
	SystemState(){}
	SystemState( int disp_w, int disp_h, float disp_aspect );

	void	SetViewport( float x, float y, float w, float h );
	void	SetFontScale( float sca_x, float sca_y );
};


//==================================================================
struct sublist_t
{
	short		prim_type;
	short		vcount;
	PSYSGFX::BasicBlend	_blendtype;
	TextureBase	*texturep;
	void		*vptr;
};

//==================================================================
struct col_vert_t
{
	float	pos[3];
	u_char	col[4];
};

//==================================================================
struct tex_vert_t
{
	float	pos[3];
	float	tex[2];
	u_char	col[4];
};

//==================================================================
#define MAX_MAPS		20	// should be plenty

//==================================================================
struct font_t
{
	SpriteManager	*si;
	int				fix_w, fix_h;
	int				spc_w, spc_h;

	u_char			*custom_mapp;

	font_t() :
		si(NULL),
		custom_mapp(NULL)
	{
	}
};

//==================================================================
#define MAX_VERTS_PER_TYPE	16384
#define MAX_SUBLISTS		4096

//==================================================================
class Context
{
public:
	col_vert_t		_col_verts[MAX_VERTS_PER_TYPE];
	tex_vert_t		_tex_verts[MAX_VERTS_PER_TYPE];
	int				_n_col_verts;
	int				_n_tex_verts;
	float			_cur_tex[2];
	u_char			_glt_cur_col[4];
	TextureBase		*_cur_texturep;
	int				_cur_blendtype;
	bool			_has_texture;

	sublist_t		_lists[MAX_SUBLISTS];
	int				_n_lists;
	sublist_t		*_listp;

	SpriteManager	*_mload_si;
	u_char			_font_use_table[GXY_MAX_FONTS];
	font_t			_font[GXY_MAX_FONTS];

	RendDevice		*_rdevicep;

	SystemState		_sys_state;

	Context( RendDevice *rdevicep, int disp_w, int disp_h, float disp_aspect );
};

};

#endif